package com.up360.rapidcalc.aerolite.util
{
    import com.up360.rapidcalc.aerolite.consts.MyGlobal;
    
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Graphics;
    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.geom.Rectangle;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    import flash.utils.Timer;
    
    /**
     * @author Tunie
     * @date   May 9, 2013
     * 文本块组件，用于显示文本
     */
    public class TextView extends Sprite
    {
        public static var delay:Number=20;
        /**
         * 文本块显示大小
         */        
        private var _width:Number;
        private var _height:Number;
        /**
         *层管理器 
         */        
        public var _layerManager:LayerManager;
        /////////////////////
        private var _mask:Shape;
        private var _content:Sprite;//内容块
        private var _bar:Sprite;//滑动块
        private var _border:DisplayObject;//边框
        private var _tf:TextField;//文本框 
        /////////////////////
        private var _target:MovieClip;
        ///////////
        private var _timer:Timer;//用于控制文本内容随着bar的动态
        //
        public function TextView(width:Number,height:Number,target:MovieClip=null)
        {
            super();
            _width=width;
            _height=height;
            _layerManager=new LayerManager(this);
            setup();
            
            _target=target;
            applayTarget();
        }
        
        private function applayTarget():void
        {
            if(_target)
            {
                x=_target.x;
                y=_target.y;
                _width=_target.width;
                _height=_target.height;
                setupMask();
                if(_target.parent)
                {
                    var b_parent:DisplayObjectContainer=_target.parent;
                    var b_deeps:int=b_parent.getChildIndex(_target);
                    b_parent.addChild(this);
                    b_parent.setChildIndex(this,b_deeps);
                    b_parent.removeChild(_target);
                }
                setBackground(_target.mcBg);
                setBorder(_target.mcBorder);
                setBar(_target.mcBar);
                //
                updContent();
            }
        }
        
        public function update():void
        {
            _tf.height=_tf.textHeight;
//            trace(_tf.height,_content.height,_tf.textHeight);
            //
            updateBar();
        }
        
        /**
         * 更新内容块 
         * @param content
         * 
         */
        public function updText(content:String):void
        {
            _tf.text=content;
            update();
        }
        
        public function appandText(content:String):void
        {
            var b_text:String=_tf.text;
            if(b_text&&b_text!="") b_text+="\n";
            b_text+=content;
            updText(b_text);
        }
        /**
         *拖动块 
         * @param obj
         * 
         */
        public function setBar(obj:Sprite):void
        {
            if(checkBar(obj))
            {
                _layerManager.add(_bar,LayerManager.OUTLOOK);
                _layerManager.top(_bar);
            }
        }
        public function updateBar():void
        {
            _bar.visible=true;
            if(_content.height<_height) _bar.visible=false;
        }
        /**
         * 设置边框 
         * @param obj
         * 
         */
        public function setBorder(obj:DisplayObject):void
        {
            if(_border)
            {
                _layerManager.remove(_border);
            }
            _border=obj;
            _layerManager.add(_border,LayerManager.OUTLOOK);
            //
            _content.x=_border.x;
            _content.y=_border.y;
        }
        /**
         *设置背景 
         * @param obj
         * 
         */
        public function setBackground(obj:DisplayObject):void
        {
            _layerManager.clearLayer();
            _layerManager.add(obj);
        }
        
        private function setup():void
        {
            setupMask();
            setupContent();
            setupTimer();
        }
        
        private function setupMask():void
        {
            if(_mask) _mask.graphics.clear();
            else _mask=new Shape();
            //
            var b_g:Graphics=_mask.graphics;
            b_g.beginFill(0);
            b_g.drawRect(0,0,_width,_height);
            b_g.endFill();
            //
            if(mask&&mask!=_mask)
            {
                removeChild(mask);
                mask=null;
            }
            mask=_mask;
            addChild(_mask);
        }
        
        private function setupContent():void
        {
            _content=new Sprite();
            _content.mouseChildren=false;
            setupTf();
            _content.addChild(_tf);
            _layerManager.add(_content,LayerManager.GAME);
        }
        
        private function updContent():void
        {
            _tf.width=_width;
            _tf.height=_tf.textHeight;
            updateBar();
        }
        
        /**
         *更新内容的位置 
         * 根据bar相对顶部位置，求出content的位置
         */
        private function updContentLocal():void
        {
            var b_local:Number=_bar.y;
            b_local/=_height-_bar.height;
            b_local*=_content.height-_height;
            _content.y=-b_local;
        }
        
        private function setupTf():void
        {
            _tf=new TextField();
            _tf.type=TextFieldType.DYNAMIC;
            _tf.wordWrap=true;
            _tf.width=_width;
            
            var b_formt:TextFormat=_tf.defaultTextFormat;
            b_formt.font="黑体";
            b_formt.color=0x109661;
            b_formt.size=22;
            b_formt.leftMargin=22;
            b_formt.leading=6;
            
            _tf.y=6;
            _tf.defaultTextFormat=b_formt;
            
        }
        
        /**
         * 只有在执行了checkBar的情况才能调用此方法，为bar添加事件监听
         */
        private function setupBar():void
        {
            _bar.addEventListener(MouseEvent.MOUSE_DOWN,barDown);
            MyGlobal.stage.addEventListener(MouseEvent.MOUSE_UP,stageUp);
        }
        
        protected function barDown(event:MouseEvent):void
        {
            _bar.startDrag(false,new Rectangle(_bar.x,0,0,_border.height-_bar.height));
            _timer.start();
        }
        
        protected function stageUp(event:MouseEvent):void
        {
            _bar.stopDrag();
            _timer.stop();
        }
        
        /**
         *检测bar,若通过检测则返回false,否则返回false 
         * @param obj
         * @return 
         * 
         */
        private function checkBar(obj:Sprite):Boolean
        {
            if(_bar)
            {
                if(_bar!=obj)
                {
                    clearBar();
                    _bar=obj;
                    _bar.buttonMode=true;
                    setupBar();
                    return true;
                }
                return false;
            }
            _bar=obj;
            _bar.buttonMode=true;
            setupBar();
            return true;
        }
        
        private function clearBar():void
        {
            _bar.removeEventListener(MouseEvent.MOUSE_DOWN,barDown);
            MyGlobal.stage.removeEventListener(MouseEvent.MOUSE_UP,stageUp);
        }
        
        private function setupTimer():void
        {
            _timer=new Timer(delay);
            _timer.addEventListener(TimerEvent.TIMER,timerTimer);
            function timerTimer(event:TimerEvent):void
            {
                updContentLocal();
            }
        }
    }
}